package com.ruoyi.common.log.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.log.domain.Cargo;
import com.ruoyi.common.log.domain.City;
import com.ruoyi.common.log.service.CargoService;
import com.ruoyi.common.log.service.CityService;
import com.ruoyi.common.log.utils.Result;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("cargo")
public class CargoController {
    @Autowired
    CargoService cargoService;


    @Autowired
    CityService cityService;

    @Autowired
    javax.servlet.http.HttpServletResponse response;


    private static String accessKey="TFUFhcf36PBh8JSC";

    private static String secretKey="c1egDsoSPQ2gcZ6PJSpTehiZGbkDyXjC";


    private static String bucketName="test";




    @RequestMapping("uploadFile")
    public String uploadFile(MultipartFile file) throws Exception {
        MinioClient minioClient = MinioClient
                .builder()
                .endpoint("http://127.0.0.1:9000")
                .credentials(accessKey, secretKey)
                .build();
        String filename = UUID.randomUUID() + file.getOriginalFilename();
        try {
            minioClient.putObject(PutObjectArgs.builder().object(filename)
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .contentType(file.getContentType()).bucket(bucketName)
                    .build());
        } catch (Exception e) {
            throw new Exception(e);
        }
        return "http://127.0.0.1:9000/" + bucketName + "/" + filename;
    }


    @RequestMapping("/excelCargo")
    public void getexcel() throws IOException {
        String fileName = URLEncoder.encode("菜单表", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<Cargo> list = cargoService.list();
        EasyExcel.write(response.getOutputStream(), Cargo.class).sheet("仓库管理").doWrite(list);
    }

    @RequestMapping("/list")
    public Result getlist(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize)
    {
        PageHelper.startPage(pageNum,pageSize);

        List<Cargo> list = cargoService.list();


        PageInfo<Cargo> cargoPageInfo = new PageInfo<>(list);

        return Result.success(cargoPageInfo);



    }


    @RequestMapping("/gettype")
    public Result gettype()
    {

        List<City> list = cityService.list();




        //配置'
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都有默认值的
        treeNodeConfig.setIdKey("value");
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setDeep(3);
        treeNodeConfig.setParentIdKey("pid");
        treeNodeConfig.setChildrenKey("children");
// 最大递归深度


//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<Object>> treeNodes = TreeUtil.<City, Object>build(list, 1, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getCityId());
                    tree.setParentId(treeNode.getPid());
                    tree.setName(treeNode.getCityname());


                });


        return Result.success(treeNodes);


    }



    @RequestMapping("/Cargoadd")
    public Result add(@RequestBody Cargo cargo)
    {
        boolean b= cargoService.saveOrUpdate(cargo);

        return b?Result.success():Result.fail();

    }


    @RequestMapping("/deleteById")
    public Result deleteById(Integer id)
    {
        boolean b = cargoService.removeById(id);


        return b?Result.success():Result.fail();

    }
}
